package com.irdstudio.apicenter.frame.core.jdbc.util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

/**
 * MACCS系统连接管理类 启动时由EntryServlet初始化，其余时候直接调用
 * 
 * @author guangming.li
 * @version 1.0
 * @date 2015-05-20
 */
public class ConnectionManager {

	/* dataSource,在初始化时为其设置值 */
	private static DataSource dataSource = null;

	/**
	 * 设置数据源
	 * 
	 * @param dataSource
	 */
	public static void setDataSource(DataSource dataSource) {
		ConnectionManager.dataSource = dataSource;
	}

	/**
	 * 获取连接
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		try {
			return dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 释放连接
	 * 
	 * @param conn
	 */
	public static void releaseConnection(Connection conn) {
		if (conn != null) {
			try {
				if (!conn.isClosed()) {
					conn.clearWarnings();
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
